#include "st1633.h"


extern I2C_HandleTypeDef hi2c1;


void delay_ms(uint32_t nms)
{
	HAL_Delay(nms);
}


u8 is_power_down = 0;
//向ST1633写入一次数据
// reg:起始寄存器地址
// buf:数据缓缓存区
// len:写数据长度
//返回值:0,成功;1,失败.
u8 ST1633_WR_Reg(u16 reg, u8 *buf, u8 len)
{
	/*u8 i;
	u8 ret = 0;
	CT_IIC_Start();
	CT_IIC_Send_Byte(ST_CMD_WR); //发送写命令
	CT_IIC_Wait_Ack();
	CT_IIC_Send_Byte(reg & 0XFF); //发送低8位地址
	CT_IIC_Wait_Ack();
	for (i = 0; i < len; i++) {
		CT_IIC_Send_Byte(buf[i]); //发数据
		ret = CT_IIC_Wait_Ack();
		if (ret)
			break;
	}
	CT_IIC_Stop(); //产生一个停止条件
	return ret;
	*/

	return HAL_I2C_Mem_Write(&hi2c1, 0xaa, reg, I2C_MEMADD_SIZE_8BIT, buf, len, 0xff);
}
//从ST1633读出一次数据
// reg:起始寄存器地址
// buf:数据缓缓存区
// len:读数据长度
void ST1633_RD_Reg(u16 reg, u8 *buf, u8 len)
{
	/*u8 i;
	CT_IIC_Start();
	CT_IIC_Send_Byte(ST_CMD_WR); //发送写命令
	CT_IIC_Wait_Ack();
	CT_IIC_Send_Byte(reg & 0XFF); //发送低8位地址
	CT_IIC_Wait_Ack();
	CT_IIC_Stop();
	delay_ms(1);
	CT_IIC_Start();
	CT_IIC_Send_Byte(ST_CMD_RD); //发送读命令
	CT_IIC_Wait_Ack();
	for (i = 0; i < len; i++) {
		buf[i] = CT_IIC_Read_Byte(i == (len - 1) ? 0 : 1); //发数据
	}
	CT_IIC_Stop(); //产生一个停止条件*/

	HAL_I2C_Mem_Read(&hi2c1, 0xaa, reg, I2C_MEMADD_SIZE_8BIT, buf, len, 0xff);
}
//问题3：获取到的X和Y的值，在哪个范围内属于正常值？
void ST_GET_RESOLUTION(void)
{
	u16 res_X, res_Y;
	u8 buffer[3] = {0};

	ST1633_RD_Reg(XY_RESOLUTION_HIGH, buffer, 3);

	res_X = ((buffer[0] & (X_RES_H_BMSK << X_RES_H_SHFT)) << 4) | buffer[1];
	res_Y = ((buffer[0] & Y_RES_H_BMSK) << 8) | buffer[2];

	printf(" touch result: res_X=%d,res_Y=%d\r\n", res_X, res_Y);
}

void ST1633_GET_REVISION(void)
{
	u8 buffer[4] = {0};
	ST1633_RD_Reg(FIRMWARE_REVISION_3, buffer, 4);

	printf("FW revision (hex) = %d %d %d %d\n", buffer[0], buffer[1], buffer[2], buffer[3]);
}

u8 ST1633_GET_MAX_TOUCH_NUM(void)
{
	u8 buf[8] = {0};
	ST1633_RD_Reg(0x3F, buf, 8);
	printf("Maximum Number of Contacts Support=%d\r\n", buf[0]);
	return buf[0];
}

u8 ST1633_GET_STATUS(void)
{
	u8 buf[8] = {0};
	ST1633_RD_Reg(ST_REG_STATUS, buf, 8);
	printf(" touch status=%d\r\n", buf[0]);
	return buf[0];
}
//初始化ST1633触摸屏
//返回值:0,初始化成功;1,初始化失败
//问题2：1633这样初始化是否可以，还有没有其它寄存器需要配置
int ST1633_Init(void)
{
	u8 result;
	u8 i;
	u8 isInitOK = 0;
	//CT_IIC_Init(); //初始化电容屏的I2C总线

	HAL_GPIO_WritePin(TP_RST_GPIO_Port, TP_RST_Pin, GPIO_PIN_SET);
	delay_ms(50);

	HAL_GPIO_WritePin(TP_RST_GPIO_Port, TP_RST_Pin, GPIO_PIN_RESET);
	delay_ms(50);

	HAL_GPIO_WritePin(TP_RST_GPIO_Port, TP_RST_Pin, GPIO_PIN_SET);
	delay_ms(50);

	for (i = 0; i < 50; i++) {
		result = ST1633_GET_STATUS();
		if (result == 0 || result == 4) {
			isInitOK = 1;
			break;
		} else if (result == 6) {
			isInitOK = 0; // bootcode status: means IC's flash
				      // checksum wrong
			break;
		} else
		delay_ms(1000);
	}

	if (isInitOK == 0)
		return -1;

	ST1633_GET_REVISION();
	ST_GET_RESOLUTION();
	ST1633_GET_MAX_TOUCH_NUM();


	return 0;
}

void ST1633_suspend()
{
	u8 buf[1];
	buf[0] = 0x2;
	ST1633_WR_Reg(0x2, buf, 1);
	is_power_down = 1;
}

void ST1633_resume()
{
	//ST_RST = 1;
	HAL_GPIO_WritePin(TP_RST_GPIO_Port, TP_RST_Pin, GPIO_PIN_SET);
	delay_ms(500);
	//ST_RST = 0; //复位
	HAL_GPIO_WritePin(TP_RST_GPIO_Port, TP_RST_Pin, GPIO_PIN_RESET);
	delay_ms(500);
	//ST_RST = 1; //释放复位
	HAL_GPIO_WritePin(TP_RST_GPIO_Port, TP_RST_Pin, GPIO_PIN_SET);
	delay_ms(9000);

	is_power_down = 0;
}
void ST1633_report_touchdown(u16 x, u16 y)
{
	// do touch down here
}
void ST1633_report_touchup(u16 x, u16 y)
{
	// do touch up here
}

extern uint8_t tpInt;

int ST1633_thread()
{
	u16 x, y;
	u8 buf[20];
	while (1) {
		//MOD
		//if (is_power_down == 0 /*&& ST_INT == 0*/) {
		if (is_power_down == 0 && tpInt == 1 ) {
			ST1633_RD_Reg(0x12, buf, 20);

			for(uint8_t i = 0; i < 5; i++) {
				if(buf[i*4] & 0x80)
				{
					printf("point %d valid\r\n", i);
					x = (u16)((buf[i*4] & 0x70) << 4 | buf[i*4+1]);
					y = (u16)((buf[i*4] & 0x07) << 8 | buf[i*4+2]);

					printf(" touch %d: X=%d,Y=%d\r\n",i, x, y);

					/*if (buf[i*4] & 0x80)
						ST1633_report_touchdown(x, y);
					else
						ST1633_report_touchup(x, y);*/
					
				}
				else
				{
					//printf("point %d not valid\r\n", i);
				}
			}
			tpInt = 0;
		}
		
		delay_ms(20);
	}
}
/*
int main(void)
{
	int result=0;
	delay_init();	    	 //延时函数初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
	uart_init(9600);	 //串口初始化为9600
	LED_Init();		  	 //初始化与LED连接的硬件接口
	delay_ms(200);
	CT_IIC_Init();

	result = ST1633_Init();
	printf(" initial result: =%d\r\n",result);
	

	

	ST1633_thread();

}*/

void st1633_tests(void)
{
	printf("Enter ST1633 test cases\r\n");

	int result = 0;
	//delay_init();					//延时函数初始化
	//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 设置中断优先级分组2
	//uart_init(9600);				//串口初始化为9600
	//LED_Init(); //初始化与LED连接的硬件接口
	//delay_ms(200);
	//CT_IIC_Init();

	result = ST1633_Init();
	printf(" initial result: =%d\r\n", result);

	ST1633_thread();
}